package edu.hawaii.duedates.parser;


/**
 * DueDatesParser deals with parsing command-line specific options for the DueDates application.
 * This class extends from the abstract class, Argument, and defines the necessary abstract methods.
 * 
 * @author Anthony M. Du
 *
 */
public class DueDatesParser extends Argument {
  // Integer containing the within num (eg: 0 or more).
  private int withinNum = 0;

  /**
   * Constructor for parsing arguments.
   * @param args - String array of passed arguments.
   */
  public DueDatesParser(String[] args) {
    parse(args);
  }

  /**
   * Checks if console option is on or off.
   * @return boolean value of true/false.
   */
  public boolean isConsole() {
    return isOptionExists("-console");
  }
  
  /**
   * Checks if email option is on or off.
   * @return boolean value of true/false.
   */
  public boolean isEmail() {
    return isOptionExists("-email");
  }
  
  /**
   * Checks if wake-up option is on or off.
   * @return boolean value of true/false.
   */
  public boolean isWakeup() {
    return isOptionExists("-wakeup");
  }
  
  /**
   * IsVerbose checks for verbose option on/off.
   * @return boolean value of true/false.
   */
  public boolean isVerbose() {
    return isOptionExists("-verbose");
  }

  /**
   * IsSort checks for sort option on/off.
   * @return boolean value of true/false.
   */
  public boolean isSort() {
    return isOptionExists("-sort"); 
  }

  /**
   * SortType parameter of sort option.
   * @return sortType - String containing the sort parameter.
   */
  public String sortType() {
    Option opt = null;
    opt = findOption("-sort");
    
    if (opt != null && opt.hasParams()) {
      return opt.getOptParams().get(0);
    }
    else { 
      return "library";
    }
  }

  /**
   * IsWithin checks for within option on/off.
   * @return boolean value of true/false.
   */
  public boolean isWithin() {
    return isOptionExists("-within");
  }

  /**
   * WithinNum parameter of within option.
   * @return withinNum - integer containing the within parameter.
   */
  public int withinNum() {
    try {
      return withinNum = Integer.parseInt(findOption("-within").getOptParams().get(0));
    }
    catch (NumberFormatException e) {
      System.err.print(findOption("-within").getOptUsage());
    }
    return withinNum;
  }

  /**
   * Checks whether or not an option passed is in fact an option.
   * This is done by checking if the first character of the option string is a hyphen '-'.
   * @param option - String containing the option being passed.
   * @return boolean of true/false.
   */
  @Override
  public boolean isOption(String option) {
    if ("".equals(option) || (option.charAt(0) == '-')) {
      return true;
    }
    return false;
  }  

  /**
   * Checks whether or not an argument passed is in fact a parameter (or not option).
   * This is done by checking if the first character of the argument string contains a hyphen '-'.
   * @param param - String containing the option being passed.
   * @return boolean of true/false.
   */
  @Override
  public boolean isParam(String param) {
    if ("".equals(param) || (param.charAt(0) != '-')) {
      return true;
    }
    return false;
  }
}
